﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Interfaces.Movie;
using Rodunu.Movies.Interfaces.Subscriptions;
using Rodunu.Movies.Interfaces.User;
using Rodunu.Movies.Logic.Movie;
using Rodunu.Movies.Logic.Subscriptions;
using Rodunu.Movies.Logic.User;
using Rodunu.Movies.Services.Amazon;
using Rodunu.Movies.Services.IMDB;
using Rodunu.Movies.Services.Interfaces;
using Rodunu.Movies.Services.Interfaces.Netflix;
using Rodunu.Movies.Services.Models.Movies;
using Rodunu.Movies.Services.NetFlix.Logic;

namespace Rodunu.Movies.Web.Controllers
{
    public class SearchController : UserBaseController
    {
        private readonly IMovie _movieLogic;
        private readonly IUserMovies _userMoviesLogic;

        public SearchController()
        {
            _movieLogic = new MovieLogic();
            _userMoviesLogic = new UserMoviesLogic();
        }

        public void SearchMovies(string MovieSearchBox)
        {
            SetSearchDefaults(MovieSearchBox);

            PropertyBag["Who"] = "My";
            PropertyBag["MovieName"] = MovieSearchBox;
            PropertyBag["UserMovies"] = _userMoviesLogic.SearchForMovies(MovieSearchBox, User.Id);
        }

        private void SetSearchDefaults(string MovieSearchBox)
        {
            SetUser();
            SetPage(PageTypes.Movies);
            SetLayout(true);

            IUser userLogic = new UserLogic();
            UserInfo currentUser = userLogic.Find(User.Id);
            ISubscriptions subscriptionsLogic = new SubscriptionsLogic();
            if (subscriptionsLogic.IsActiveService(currentUser, ExternalServices.Netflix) && !String.IsNullOrEmpty(currentUser.NetflixId))
                PropertyBag["IsNetflix"] = true;
            else
                PropertyBag["IsNetflix"] = false;

            PropertyBag["SearchVariable"] = MovieSearchBox;
            PropertyBag["CurrentUser"] = User.Id;
        }

        public void SearchFriendsMovies(string movie)
        {
            SetSearchDefaults(movie);

            PropertyBag["UserMovies"] = _userMoviesLogic.SearchForFriendsMovies(movie, User.Id);
        }

        public void AllMovies(string movie)
        {
            SetSearchDefaults(movie);

            PropertyBag["Movies"] = _movieLogic.SearchForMovies(movie).OrderBy(o => o.Name).ToList();
        }

        public void SearchAmazon(string title, long id)
        {
            SetUser();
            IAmazonService amazonService = new AmazonService();
            AmazonSearchResult movies = amazonService.SearchWebService(title, 0, 1, true, false);
            PropertyBag["AmazonMovies"] = movies.Titles;
            PropertyBag["MovieId"] = id;
            IUser userLogic = new UserLogic();
            UserInfo userInfo = userLogic.Find(User.Id);
            PropertyBag["IsAdmin"] = userInfo.IsAdmin;
        }

        public void SearchAmazonMovies(string movie)
        {
            SetSearchDefaults(movie);

            IAmazonService amazonService = new AmazonService();
            AmazonSearchResult movies = amazonService.SearchWebService(movie, 0, 1, true, false);
            PropertyBag["Movies"] = movies.Titles;
        }

        public void SearchNetflixMovies(string movie)
        {
            SetSearchDefaults(movie);

            IUser userLogic = new UserLogic();
            UserInfo userInfo = userLogic.Find(User.Id);
            ISearch search = new Search(userInfo);
            PropertyBag["Movies"] = search.SearchCatalog(movie, 0, 20).Titles;
        }

        public void SearchNetflix(string title, long id)
        {
            SetUser();
            PropertyBag["RodMovieId"] = id;
            IUser userLogic = new UserLogic();
            UserInfo userInfo = userLogic.Find(User.Id);
            ISearch search = new Search(userInfo);
            PropertyBag["Movies"] = search.SearchCatalog(title, 0, 20).Titles;
            PropertyBag["IsAdmin"] = userInfo.IsAdmin;

            ISubscriptions subscriptionsLogic = new SubscriptionsLogic();
            UserInfo currentUser = userLogic.Find(User.Id);
            if (subscriptionsLogic.IsActiveService(currentUser, ExternalServices.Netflix) && !String.IsNullOrEmpty(currentUser.NetflixId))
                PropertyBag["IsNetflix"] = true;
            else
                PropertyBag["IsNetflix"] = false;
        }
    }
}
